package com.zxl.live.screen.call.ui.widget;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zxl.live.screen.R;
import com.zxl.live.screen.call.model.bean.ThemeBean;
import com.zxl.live.tools.tools.LogUtils;

import java.io.IOException;

import pl.droidsonroids.gif.GifDrawable;

/**
 * Created by Administrator on 2017/12/8.
 */
public class ThemePreviewWindow extends FrameLayout {

    private ImageView mCoverView;

    private VideoPlayerView mVideoPlayerView;

    private GifAnimationView mGifAnimationView;

    private CallUseInfoWidget mCallUseInfoWidget;

    public ThemePreviewWindow(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        inflate(getContext(), R.layout.widget_video_preview, this);
        mCallUseInfoWidget = (CallUseInfoWidget) findViewById(R.id.use_info);
        mCoverView = (ImageView) findViewById(R.id.animation_cover);
        mVideoPlayerView = (VideoPlayerView) findViewById(R.id.animation_view);
        mGifAnimationView = (GifAnimationView) findViewById(R.id.animation_gif_view);
    }

    public void refreshTheme(ThemeBean themeBean, int defaultWidth, int defaultHeight) {
        mCallUseInfoWidget.refreshTheme(themeBean);
        Glide.with(getContext()).load(themeBean.banner).override(defaultWidth, defaultHeight)
                .placeholder(new ColorDrawable(Color.parseColor(themeBean.defaultColor))).into(mCoverView);
    }

    public void setDefaultNumber() {
        mCallUseInfoWidget.setDefaultNumber();
    }


    public void showPreview(boolean isShow) {
        mVideoPlayerView.setVisibility(isShow ? View.VISIBLE : View.GONE);
        mGifAnimationView.setVisibility(isShow ? View.VISIBLE : View.GONE);
    }

    public void play(final ThemeBean themeBean) {
        LogUtils.d("manager play " + themeBean.title);
        play(themeBean.type, themeBean.getDownloadPath());
    }

    public void play(String type, String path) {
        if (ThemeBean.TYPE_MP4.equals(type)) {
            mVideoPlayerView.setVisibility(View.VISIBLE);
            mGifAnimationView.setVisibility(View.GONE);
            mVideoPlayerView.play(path);
        } else if (ThemeBean.TYPE_GIF.equals(type)) {
            try {
                mGifAnimationView.setVisibility(View.VISIBLE);
                mVideoPlayerView.setVisibility(View.GONE);
                mGifAnimationView.setImageDrawable(new GifDrawable(path));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void stop() {
        LogUtils.d("manager stop");
        mVideoPlayerView.stop();
        mGifAnimationView.stop();
    }

    public ImageView getmIconView() {
        return mCallUseInfoWidget.getmIconView();
    }

    public TextView getmNameView() {
        return mCallUseInfoWidget.getmNameView();
    }

    public TextView getmNumberView() {
        return mCallUseInfoWidget.getmNumberView();
    }

    public void setNumber(String number) {
        mCallUseInfoWidget.setNumber(number);
    }


}
